package com.gardenguell.salad.data.volley;


import android.widget.Toast;
import com.android.volley.VolleyError;
import com.gardenguell.salad.R;
import com.gardenguell.salad.app.AppConstant;
import com.gardenguell.salad.app.AppContext;
import com.gardenguell.salad.app.HandlerCenter;
import com.gardenguell.salad.data.util.Response;
import com.gardenguell.salad.util.CommonLog;
import com.google.gson.*;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by absurd on 2015/12/5.
 */
public class SaladRequest<T> extends  GsonRequest<Response>{

    public SaladRequest(String url, final Type type,
                        final OnSuccessResponse<T> onSuccessResponse, final OnErrorResponse onErrorResponse){
        super(Method.GET, url, Response.class, null, null,
                new com.android.volley.Response.Listener<Response>() {

                    private void showNetworkError(int strId){
                        CommonLog log = CommonLog.getInstance();
                        Toast.makeText(AppContext.getContext(), strId, Toast.LENGTH_SHORT).show();
                        log.d(AppConstant.LOG_TAG.NETWORK_ERROR + "---------> " + R.string.error_undefined);
                    }
                    @Override
                    public void onResponse(Response response) {
                        // do the default actions!!
                        if(response.getStatus() != 200) {
                            onErrorResponse.onError(response.getStatus());
                            if (response.getStatus() == 300) {
                                showNetworkError(R.string.error_undefined);
                                return;
                            } else if (response.getStatus() == 301) {
                                showNetworkError(R.string.error_no_data);
                                return;
                            } else if (response.getStatus() == 302) {
                                showNetworkError(R.string.error_wrong_token);
                                HandlerCenter.getInstance().getHandler(HandlerCenter.Flag.BASE_ACTIVITY)
                                        .sendEmptyMessage(HandlerCenter.Message.WRONG_TOKEN);
                                return;
                            } else if (response.getStatus() == 303) {
                                showNetworkError(R.string.error_user_exist);
                                return;
                            } else if (response.getStatus() == 304) {
                                showNetworkError(R.string.error_wrong_username);
                                return;
                            } else if (response.getStatus() == 305) {
                                showNetworkError(R.string.error_wrong_password);
                                return;
                            } else if (response.getStatus() == 306) {
                                showNetworkError(R.string.error_unit_not_free);
                                return;
                            }else if(response.getStatus() == 307){
                                showNetworkError(R.string.error_mac_offline);
                                return;
                            }
                        }
                        GsonBuilder gsonBuilder = new GsonBuilder();
                        gsonBuilder.setDateFormat("yyyy-MM-dd HH:mm:ss");
                        Gson gson = gsonBuilder.create();
                        JsonParser parser = new JsonParser();
                        if(response.getData() == null){
                            onSuccessResponse.onSuccessWithNoBody();
                            return;
                        }
                        JsonElement jsonContent = parser.parse(response.getData());
                        if(jsonContent.isJsonNull()){
                            onSuccessResponse.onSuccessWithNoBody();
                        }
                        else if (jsonContent.isJsonArray()) {
                            JsonArray jsonArray = parser.parse(response.getData()).getAsJsonArray();
                            List<T> res = new ArrayList<T>();
                            for (JsonElement element : jsonArray) {
                                T obj = gson.fromJson(element, type);
                                res.add(obj);
                            }
                            onSuccessResponse.onSuccessWithList(res);
                        } else {
                            T obj = gson.fromJson(jsonContent, type);
                            onSuccessResponse.onSuccessWithObj(obj);
                        }
                    }
                }, new com.android.volley.Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // do the default actions!!!
                        error.printStackTrace();
                        Toast.makeText(AppContext.getContext(), R.string.error_volley, Toast.LENGTH_SHORT).show();
                        onErrorResponse.onError(-1);
                    }
                });
    }


    public SaladRequest(String url, final Type type, String requestBody,
                        final OnSuccessResponse<T> onSuccessResponse, final OnErrorResponse onErrorResponse){
        super(Method.POST, url, Response.class, null, requestBody,
                new com.android.volley.Response.Listener<Response>(){
                    private void showNetworkError(int strId){
                        CommonLog log = CommonLog.getInstance();
                        Toast.makeText(AppContext.getContext(), strId, Toast.LENGTH_SHORT).show();
                        log.d(AppConstant.LOG_TAG.NETWORK_ERROR + "---------> " + R.string.error_undefined);
                    }
                    @Override
                    public void onResponse(Response response) {
                        // do the default actions!!
                        // why won't fall in this callback ??
                        // if the response is null, the deliverResponse will not be called!
                        // onSuccessResponse.onSuccess(response);
                        if(response.getStatus() != 200) {
                            onErrorResponse.onError(response.getStatus());
                            if (response.getStatus() == 302) {
                                showNetworkError(R.string.error_wrong_token);
                                return;
                            }
                        }
                        onSuccessResponse.onSuccessWithNoBody();
                    }
                }, new com.android.volley.Response.ErrorListener(){
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // do the default actions!!
                        error.printStackTrace();
                        onErrorResponse.onError(-1);
                    }
                });
    }

}
